package run.halo.app.model.entity;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Lob;
import javax.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.GenericGenerator;
import run.halo.app.model.enums.SiteStatus;

/**
 * Site entity.
 *
 * @author meazty
 */
@Data
@Entity(name = "Site")
@Table(name = "sites", indexes = {
    @Index(name = "sites_create_time", columnList = "create_time")})
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER,
    columnDefinition = "int default 0")
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class Site extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "custom-id")
    @GenericGenerator(name = "custom-id", strategy = "run.halo.app.model.entity.support"
        + ".CustomIdGenerator")
    private Integer id;

    /**
     * Site name.
     */
    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "slug", unique = true)
    private String slug;

    @Column(name = "icon")
    private String icon;

    @Column(name = "target")
    private String target;

    @Column(name = "cover", length = 1023)
    private String cover;

    /**
     * Site status.
     */
    @Column(name = "status")
    @ColumnDefault("0")
    private SiteStatus status;

    /**
     * Site url.
     */
    @Column(name = "url")
    private String url;

    /**
     * Site summary.
     */
    @Column(name = "summary")
    @Lob
    private String summary;

    /**
     * raw content
     */
    @Column(name = "raw_content")
    @Lob
    private String rawContent;

    /**
     * Rendered content.
     */
    @Column(name = "content")
    private String content;

    /**
     * Whether to top the Site.
     */
    @Column(name = "priority")
    @ColumnDefault("0")
    private Integer priority;

    /**
     * Site visits.
     */
    @Column(name = "visits")
    @ColumnDefault("0")
    private Long visits;

    /**
     * Likes.
     */
    @Column(name = "likes")
    @ColumnDefault("0")
    private Long likes;

    /**
     * Rendered remark.
     */
    @Column(name = "remark")
    private String remark;

    @Override
    public void prePersist() {
        super.prePersist();

        if (status == null) {
            status = SiteStatus.PUBLISHED;
        }

        if (summary == null) {
            summary = "";
        }

        if (target == null) {
            target = "_self";
        }

        if (cover == null) {
            cover = "";
        }

        if (rawContent == null) {
            this.rawContent = "";
            this.content = "";
        }

        if (target == null) {
            target = "_self";
        }

        if (visits == null || visits < 0) {
            visits = 0L;
        }

        if (likes == null || likes < 0) {
            likes = 0L;
        }
    }

    @Override
    protected void preUpdate() {
        super.preUpdate();
    }
}